package cn.jtcoding.user.web.controller;

import cn.jtcoding.user.api.UserService;
import cn.jtcoding.user.pojo.bo.UserBO;
import cn.jtcoding.user.pojo.dataobject.UsersDO;
import cn.jtcoding.common.framework.exception.ApiException;
import cn.jtcoding.common.framework.vo.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/usernameIsExist")
    public boolean verifyUsernameIsExists(String username) {
        if (StringUtils.isBlank(username)) {
            throw new ApiException();
        }
        if (userService.verifyUsernameIsExists(username)) {
            throw new ApiException("用户已存在");
        }
        return true;
    }

    @PostMapping("/register")
    public ApiResult<?> addUser(@RequestBody UserBO userBO) {
        // 判断用户名密码不能为空
        // 判断用户名是否存在
        // 密码长度不能小于6位
        // 判断两次密码是否一致
        UsersDO user = userService.createUser(userBO);
        return ApiResult.success();
    }

    @PostMapping("/login")
    public ApiResult<?> login(@RequestBody UserBO userBO) {
        UsersDO user = userService.login(userBO);
        return ApiResult.success();
    }
}
